package com.pai4j.aigc.context.model;

import com.pai4j.domain.dto.llm.ChatCompletionMessage;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 映射工具：在 ChatMessageContext 与 ChatCompletionMessage 之间转换
 */
public final class ChatMessageMapper {

    private ChatMessageMapper() {}

    public static ChatCompletionMessage toCompletion(ChatMessageContext ctx) {
        if (ctx == null) return null;
        ChatCompletionMessage m = new ChatCompletionMessage();
        m.setRole(ctx.getRole());
        m.setName(ctx.getName());
        m.setContent(ctx.getContent());
        m.setPartial(ctx.getPartial());
        return m;
    }

    public static ChatMessageContext toContext(ChatCompletionMessage m) {
        if (m == null) return null;
        ChatMessageContext ctx = new ChatMessageContext();
        ctx.setRole(m.getRole());
        ctx.setName(m.getName());
        ctx.setContent(m.getContent());
        ctx.setPartial(m.getPartial());
        return ctx;
    }

    public static List<ChatCompletionMessage> toCompletionList(List<ChatMessageContext> ctxList) {
        if (ctxList == null || ctxList.isEmpty()) return Collections.emptyList();
        return ctxList.stream().map(ChatMessageMapper::toCompletion).filter(Objects::nonNull).collect(Collectors.toList());
    }

    public static List<ChatMessageContext> toContextList(List<ChatCompletionMessage> msgList) {
        if (msgList == null || msgList.isEmpty()) return Collections.emptyList();
        return msgList.stream().map(ChatMessageMapper::toContext).filter(Objects::nonNull).collect(Collectors.toList());
    }
}
